package entities;

import java.io.Serializable;

import javax.persistence.*;

import ejb.dto.FlightDTO;

import java.util.Date;
import java.util.List;


/**
 * The persistent class for the flights database table.
 * 
 */
@Entity
@Table(name="flights", uniqueConstraints=@UniqueConstraint(columnNames={"DEPARTURE_ID", "LANDING_ID", "DEPARTURE_TIME"}))
@NamedQueries({
	@NamedQuery(name="Flight.findAll", query="SELECT f FROM Flight f"),
	@NamedQuery(name="Flight.findFlightsTo", query="SELECT f FROM Flight f WHERE f.airport2 IN :a"),
	@NamedQuery(name="Flight.findFlightsFrom", query="SELECT f FROM Flight f WHERE f.airport1 IN :a"),
	@NamedQuery(name="Flight.findFlightsToInPeriod", query="SELECT f FROM Flight f WHERE f.airport2 IN :a AND f.departureTime>=:s AND f.departureTime<=:e"),
	@NamedQuery(name="Flight.findFlightsFromInPeriod", query="SELECT f FROM Flight f WHERE f.airport1 IN :a AND f.departureTime>=:s AND f.departureTime<=:e")
})
public class Flight implements Serializable {
	private static final long serialVersionUID = 1L;

	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	private int id;

	private String company;
	
	private float price;
	private int remaining;
	

	@Temporal(javax.persistence.TemporalType.TIMESTAMP)
	@Column(name="DEPARTURE_TIME")
	private Date departureTime;

	@Temporal(javax.persistence.TemporalType.TIMESTAMP)
	@Column(name="LANDING_TIME")
	private Date landingTime;

	//bi-directional many-to-one association to Airport
	@ManyToOne
	@JoinColumn(name="DEPARTURE_ID")
	private Airport airport1;

	/**
	 * bi-directional many-to-one association to Excursion
	 */
	@ManyToOne
	@JoinColumn(name="LANDING_ID")
	private Airport airport2;

	/**
	 * bi-directional many-to-one association to Reservation
	 */
	@OneToMany(mappedBy="flight1")
	private List<Reservation> reservations1;

	/**
	 * bi-directional many-to-one association to Reservation
	 */
	@OneToMany(mappedBy="flight2")
	private List<Reservation> reservations2;

	//CONSTRUCTORS
	public Flight() {
	}
	
	public Flight(FlightDTO flight, Airport airport1, Airport airport2) {
		this.id = flight.getId();
		this.company = flight.getCompany();
		this.departureTime = flight.getDepartureTime();
		this.landingTime = flight.getLandingTime();
		this.airport1 = airport1;
		this.airport2 = airport2;
		this.price = flight.getPrice();
		this.remaining = flight.getRemaining();
	}

	
	//GETTER AND SETTER
	public int getId() {
		return this.id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getCompany() {
		return this.company;
	}

	public void setCompany(String company) {
		this.company = company;
	}

	public Date getDepartureTime() {
		return this.departureTime;
	}

	public void setDepartureTime(Date departureTime) {
		this.departureTime = departureTime;
	}

	public Date getLandingTime() {
		return this.landingTime;
	}

	public void setLandingTime(Date landingTime) {
		this.landingTime = landingTime;
	}

	public Airport getAirport1() {
		return this.airport1;
	}

	public void setAirport1(Airport airport1) {
		this.airport1 = airport1;
	}

	public Airport getAirport2() {
		return this.airport2;
	}

	public void setAirport2(Airport airport2) {
		this.airport2 = airport2;
	}

	public List<Reservation> getReservations1() {
		return this.reservations1;
	}

	public void setReservations1(List<Reservation> reservations1) {
		this.reservations1 = reservations1;
	}


	public List<Reservation> getReservations2() {
		return this.reservations2;
	}

	public void setReservations2(List<Reservation> reservations2) {
		this.reservations2 = reservations2;
	}

	public float getPrice() {
		return price;
	}

	public void setPrice(float price) {
		this.price = price;
	}

	public int getRemaining() {
		return remaining;
	}

	public void setRemaining(int remaining) {
		this.remaining = remaining;
	}

	//METHODS
	
	/**
	 * add Reservation to list
	 * @param reservations1
	 * @return list of Reservation
	 */
	public Reservation addReservations1(Reservation reservations1) {
		getReservations1().add(reservations1);
		reservations1.setFlight1(this);

		return reservations1;
	}

	/**
	 * remove Reservation from list
	 * @param reservations1
	 * @return list of Reservation
	 */
	public Reservation removeReservations1(Reservation reservations1) {
		getReservations1().remove(reservations1);
		reservations1.setFlight1(null);

		return reservations1;
	}
	
	/**
	 * add Reservation to list
	 * @param reservations2
	 * @return list of Reservation
	 */
	public Reservation addReservations2(Reservation reservations2) {
		getReservations2().add(reservations2);
		reservations2.setFlight2(this);

		return reservations2;
	}

	/**
	 * remove Reservation from list
	 * @param reservations2
	 * @return list of Reservation
	 */
	public Reservation removeReservations2(Reservation reservations2) {
		getReservations2().remove(reservations2);
		reservations2.setFlight2(null);

		return reservations2;
	}
}